package com.example.tnevaluate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tnevaluate.mapper.CommentMapper;
import com.example.tnevaluate.service.ICommentService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yc.biz.BizException;
import com.yc.pojo.Comment;
import com.yc.pojo.CommentDTO;
import com.yc.pojo.House;
import com.yc.pojo.User;
import com.yc.util.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 廖老师
 * @since 2023-07-28
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    CommentMapper commentMapper;

    public List selectComments(Integer houseId) throws BizException {
        Utils.checkEmpty(houseId,"系统繁忙，请稍后重试!");
        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<CommentDTO>()
                .selectAll(Comment.class)
                .selectAll(User.class)
                .innerJoin(User.class, User::getId, Comment::getCreaterId)
                .eq(Comment::getHouseId, houseId)
                .orderByDesc(Comment::getCreateTime);
        return commentMapper.selectJoinList(CommentDTO.class, mpjLambdaWrapper);
    }

    public void insertComment(Integer houseId, String content, Integer userId) throws BizException {
        Utils.checkEmpty(userId, "评论前请先登录!");
        Utils.checkEmpty(content, "评论内容不能为空!");
        Utils.checkEmpty(houseId, "系统繁忙请稍后重试!");
        Comment comment = new Comment();
        comment.setHouseId(houseId);
        comment.setContent(content);
        comment.setCreaterId(userId);
        commentMapper.insert(comment);
    }
}
